package com.nf.dao.impl;

import com.nf.dao.HallInfoDao;
import com.nf.entity.HallInfo;
import com.nf.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class HallInfoDaoImpl implements HallInfoDao {
    @Override
    public List<HallInfo> selectAll() {
        List<HallInfo> list=new ArrayList<HallInfo>();

        Connection conn= DBUtil.getConnection();
        String sql="select * from hallInfo";
        PreparedStatement pstmt=null;
        ResultSet rst=null;

        try {
            pstmt=conn.prepareStatement(sql);
            rst=pstmt.executeQuery();
            while (rst.next()){
                int id=rst.getInt(1);
                String name=rst.getString(2);
                String type=rst.getString(3);
                int number=rst.getInt(4);
                String rowsCols=rst.getString(5);

                HallInfo hall=new HallInfo(id,name,type,number,rowsCols);
                list.add(hall);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(rst,pstmt,conn);
        }
        return list;
    }

    @Override
    public int del(int hallId) {
        int r=0;

        Connection conn=DBUtil.getConnection();
        String sql="delete from hallInfo where hall_id=?";
        PreparedStatement pstmt=null;
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,hallId);

            r=pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(null,pstmt,conn);
        }

        return r;
    }

    @Override
    public int add(HallInfo hall) {
        int r=0;

        Connection conn=DBUtil.getConnection();
        String sql="insert into hallInfo values(?,?,?,?)";
        PreparedStatement pstmt=null;
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,hall.getHallName());
            pstmt.setString(2,hall.getHallType());
            pstmt.setInt(3,hall.getHallNumber());
            pstmt.setString(4, hall.getRowsCols());

            r=pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(null,pstmt,conn);
        }

        return r;
    }

    @Override
    public int update(HallInfo hall) {
        int r=0;

        Connection conn=DBUtil.getConnection();
        String sql="update hallInfo set hall_name=?,hall_type=?,hall_number=?,rows_cols=? where hall_id=?";
        PreparedStatement pstmt=null;
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1, hall.getHallName());
            pstmt.setString(2, hall.getHallType());
            pstmt.setInt(3,hall.getHallNumber());
            pstmt.setString(4, hall.getRowsCols());
            pstmt.setInt(5,hall.getHallId());

            r=pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(null,pstmt,conn);
        }

        return r;
    }
}
